package com.example.administrator.kaohedemo.Activity;

import android.content.Intent;
import android.os.Handler;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.OrientationHelper;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.example.administrator.kaohedemo.Action.Actions;
import com.example.administrator.kaohedemo.Action.CourseListAction;
import com.example.administrator.kaohedemo.Adapter.CourseListAdapter;
import com.example.administrator.kaohedemo.R;
import com.example.administrator.kaohedemo.Store.CourseListStore;
import com.example.administrator.kaohedemo.Store.Store;
import com.example.administrator.kaohedemo.Utils.HttpUtils;
import com.example.administrator.kaohedemo.View.TagerLinearLayout;
import com.example.administrator.kaohedemo.View.TypePopView;
import com.squareup.otto.Subscribe;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/6/21.
 */
public class CourseListActivity extends BaseActivity implements SwipeRefreshLayout.OnRefreshListener {

    private SwipeRefreshLayout swiperefreshlayout;
    private RecyclerView recyclerview;
    private CourseListAction courseListAction;
    private TypePopView typePopView;
    private CourseListAdapter adapter;
    private TagerLinearLayout tagerlinearlayout;
    private String stageid="0";
    private String studyid="0";
    private String type="0";
    private String segid="0";
    private int pageindex=1;
    private String name="";
    private boolean scrollListener_flag=true;
    private String result;
    private TextView title_left;
    private TextView title_right;

    @Override
    protected Store getStore() {
        return CourseListStore.getInstense();
    }

    @Override
    protected void initView() {
        setContentView(R.layout.activity_courselist);
        courseListAction = CourseListAction.getInstense();

        title_left= (TextView) findViewById(R.id.title_left);
        TextView title= (TextView) findViewById(R.id.title);
        title.setText("课程列表");
        title_right= (TextView) findViewById(R.id.title_right);
        title_right.setText("看课记录");

        tagerlinearlayout = (TagerLinearLayout) findViewById(R.id.tagerlinearlayout);
        tagerlinearlayout.setTagerList(getTagerList(new ArrayList<String>()));
        typePopView = TypePopView.getInstense(this);
        typePopView.SetTargetView(tagerlinearlayout);

        swiperefreshlayout = (SwipeRefreshLayout) findViewById(R.id.swiperefreshlayout);
        swiperefreshlayout.setProgressBackgroundColorSchemeResource(android.R.color.white);
        swiperefreshlayout.setColorSchemeResources(android.R.color.holo_blue_light,
                android.R.color.holo_red_light, android.R.color.holo_orange_light,
                android.R.color.holo_green_light);
        swiperefreshlayout.setSize(SwipeRefreshLayout.LARGE);

        recyclerview = (RecyclerView) findViewById(R.id.recyclerview);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(OrientationHelper.VERTICAL);
        recyclerview.setLayoutManager(linearLayoutManager);
        adapter = new CourseListAdapter(this);
        recyclerview.setAdapter(adapter);
    }

    public List<String> getTagerList(List<String> list) {
        list.add("学段");
        list.add("学课");
        list.add("类型");
        list.add("阶段");
        return list;
    }

    @Override
    protected void initListener() {
        title_left.setOnClickListener(this);
        title_right.setOnClickListener(this);
        swiperefreshlayout.setOnRefreshListener(this);
        recyclerview.addOnScrollListener(listener);
    }

    Handler handler = new Handler();

    @Override
    protected void initData() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                result = HttpUtils.getInstense().getResult("http://mobile.yanxiu.com/test/api/guopei/myCourseList?stageid="+stageid+"&studyid="+studyid+"&os=android&pid=1289&ver=1.7.1&type="+type+"&segid="+segid+"&token=97ed20f93925c5ed9746551c915d0578&pagesize=10&pageindex="+pageindex);
                handler.post(new Runnable() {
                    @Override
                    public void run() {
                        swiperefreshlayout.setRefreshing(false);
                        courseListAction.SendResultFromHttp(Actions.CourseListActions.TYPE_HTTP_RESULT, Actions.CourseListActions.KEY_HTTP_RESULT, result);
                    }
                });
            }
        }) {
        }.start();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.title_left:
                this.finish();
                break;
            case R.id.title_right:
                startActivity(new Intent(this,SeeClassRecordActivity.class));
                break;
        }
    }

    @Subscribe
    public void CourseListStoreChanged(CourseListStore.CourseListStoreChanged cc) {
        switch (cc.getType()) {
            case Actions.CourseListActions.TYPE_ITEM_ONCLICK:
                //选中课程
                break;
            case Actions.CourseListActions.TYPE_TAGER_ONCLICK:
                //选中筛选类型
                int position = cc.getPosition();
                switch (position) {
                    case 0:
                        typePopView.setAdapterChanged(0);
                        name="segments";
                        break;
                    case 1:
                        typePopView.setAdapterChanged(1);
                        name="studys";
                        break;
                    case 2:
                        typePopView.setAdapterChanged(2);
                        name="types";
                        break;
                    case 3:
                        typePopView.setAdapterChanged(3);
                        name="stages";
                        break;
                }
                typePopView.ShowViewDown();
                break;
            case Actions.CourseListActions.TYPE_POP_DISMISS:
            case Actions.CourseListActions.TYPE_TAGER_DESTORYONCLICK:
                //pop dismiss
                tagerlinearlayout.setDestorySelect();
                break;
            case Actions.CourseListActions.TYPE_POP_ONCLICK:
                //选中筛选条件，课程列表刷新
                typePopView.SetDismiss();
                if (name.equals("segments")){
                    segid=cc.getId();
                }else if (name.equals("studys")){
                    studyid=cc.getId();
                }else if (name.equals("types")){
                    type=cc.getId();
                }else if (name.equals("stages")){
                    stageid=cc.getId();
                }
                initData();
                break;
            case Actions.CourseListActions.TYPE_HTTP_RESULT:
                //数据初始化
                typePopView.SetList(cc.getBean().body.segments,0);
                typePopView.SetList(cc.getBean().body.studys,1);
                typePopView.SetList(cc.getBean().body.types,2);
                typePopView.SetList(cc.getBean().body.stages,3);
                scrollListener_flag=true;
                if (pageindex==1){
                    adapter.setList(cc.getBean().body.modules, Actions.CourseListActions.RESULT_REFRESH);
                }else {
                    adapter.setList(cc.getBean().body.modules, Actions.CourseListActions.RESULT_MORE);
                }
                break;
        }
    }

    @Override
    public void onRefresh() {
        pageindex=1;
        initData();
    }

    private RecyclerView.OnScrollListener listener=new RecyclerView.OnScrollListener() {
        @Override
        public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
            super.onScrollStateChanged(recyclerView, newState);
            if (scrollListener_flag) {
                LinearLayoutManager manager = (LinearLayoutManager) recyclerView.getLayoutManager();
                if (manager.findLastVisibleItemPosition() == adapter.getItemCount() - 1) {
                    scrollListener_flag = false;
                    pageindex++;
                    initData();
                }
            }
        }

        @Override
        public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
            super.onScrolled(recyclerView, dx, dy);
        }
    };
}
